package com.music.controller;
/**
 * @className: AdminController
 * @description:
 * @author: 林鑫海
 * @date: 2022/7/5
 **/
import com.github.pagehelper.PageInfo;
import com.music.dto.Result;

import com.music.entity.Admin;
import com.music.service.AdminService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {
    private static final Logger logger = LoggerFactory.getLogger(AdminController.class);

    @Autowired
    private AdminService adminService;

    @GetMapping("/Phone")
    public @ResponseBody Result existOfPhoneNumber(@RequestParam String adminPhone){
        Result result = new Result();
        boolean flag = adminService.existOfPhoneNumber(adminPhone);
        return  result.ok(flag);
    }


    @PostMapping("/login")
    public @ResponseBody Result login(@RequestParam String adminPhone,@RequestParam String adminPassword){
        Result result = new Result();
        Admin admin = adminService.login(adminPhone, adminPassword);
        if(admin != null){
            result.ok(admin);
        }else {
            result.fail(0,"登录失败");
        }
        return result;
    }

    @GetMapping("/logout")
    public @ResponseBody Result logout(){
        Result result = new Result();
        adminService.logout();
        return  result.ok(true);
    }

    @GetMapping("/session")
    public @ResponseBody Result getUserBySession(){
        Result result = new Result();
        Admin adminBySession = adminService.getAdminBySession();
        if (adminBySession!=null){
            result.ok(adminBySession);
        }else {
            result.fail(0,"无数据");
        }
        return result;
    }

    @GetMapping("/adminPageList")
    public @ResponseBody Result getPageAdmin(String adminPhone,int pageNum,int pageSize){
        Result result = new Result();
        Admin admin = new Admin();
        admin.setAdminPhone(adminPhone);
        PageInfo<Admin> byPage = adminService.getAdminByPage(pageNum, pageSize, admin);
        if (byPage!=null){
            return result.ok(byPage);
        }
       return result.fail(0,"无数据");
    }

    @PostMapping("/addAdmin")
    public @ResponseBody Result addOperateAdmin(@RequestParam String adminName,@RequestParam String adminPhone,@RequestParam String adminPassword){
        Result result = new Result();
        Admin admin = new Admin();
        admin.setAdminPhone(adminPhone);
        admin.setAdminName(adminName);
        admin.setAdminPassword(adminPassword);
        //调用业务层
        boolean a = adminService.addAdmin(admin);
        if (a){
            result.ok(true).setMessage("添加成功");
        }else {
            result.fail(0,"添加失败,可能原因是它已存在");
        }
        return result;
    }

    @PostMapping("/updateAdmin")
    public @ResponseBody Result updateOperateAdmin(@RequestParam String adminName,@RequestParam String adminPhone,@RequestParam String adminPassword){
        Result result = new Result();
        Admin admin = new Admin();
        admin.setAdminPhone(adminPhone);
        admin.setAdminName(adminName);
        admin.setAdminPassword(adminPassword);
        //调用业务层
        boolean m = adminService.modifyAdmin(admin);
        if (m){
            result.ok(true).setMessage("修改成功");
        }else {
            result.fail(0,"修改失败,可能原因是它不存在");
    }
        return result;
    }

    @GetMapping("/deleteByAdminId")
    public @ResponseBody Result deleteByAdminId(@RequestParam Integer adminId){
        Result result = new Result();
        Admin admin = new Admin();
        admin.setAdminId(adminId);
        //调用业务层
        int delete = adminService.deleteByAdminId(adminId);
        if (delete>0){
            result.ok(true).setMessage("删除成功");
        }else {
            result.fail(0,"删除失败,可能原因是它不存在，或者被关联引用");
        }
        return result;
    }
}